Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
The tsutils package provides a set of utility functions for working with TypeScript. It is particularly useful for developers building TypeScript tools and plugins, as it offers a variety of functions to manipulate and analyze TypeScript code.
Type Guards
This feature provides type guard functions to check the type of TypeScript nodes. In the example, `isVariableStatement` is used to check if a node is a variable statement.
const ts = require('typescript');
const tsutils = require('tsutils');
const node = ts.createSourceFile('example.ts', 'let x: number = 42;', ts.ScriptTarget.Latest);
if (tsutils.isVariableStatement(node.statements[0])) {
console.log('This is a variable statement');
}
AST Utilities
AST (Abstract Syntax Tree) utilities help in traversing and manipulating the AST of TypeScript code. The example demonstrates how to find all variable declarations in a source file.
const ts = require('typescript');
const tsutils = require('tsutils');
const sourceFile = ts.createSourceFile('example.ts', 'let x: number = 42;', ts.ScriptTarget.Latest);
const variableDeclarations = tsutils.findVariableDeclarations(sourceFile);
console.log(variableDeclarations);
Type Utilities
Type utilities provide functions to work with TypeScript types. The example shows how to check if a variable's type is a number using `isTypeFlagSet`.
const ts = require('typescript');
const tsutils = require('tsutils');
const program = ts.createProgram(['example.ts'], {});
const checker = program.getTypeChecker();
const sourceFile = program.getSourceFile('example.ts');
sourceFile.forEachChild(node => {
if (ts.isVariableDeclaration(node)) {
const type = checker.getTypeAtLocation(node);
console.log(tsutils.isTypeFlagSet(type, ts.TypeFlags.Number)); // true if the type is a number
}
});
typescript-eslint is a set of tools for using ESLint with TypeScript. It includes a parser and a set of rules that help in linting TypeScript code. While tsutils focuses on utility functions for TypeScript AST and types, typescript-eslint is more about integrating TypeScript with ESLint for code quality and style enforcement.
ts-morph is a library for TypeScript compiler API wrapper. It simplifies the process of working with the TypeScript AST and provides a higher-level API compared to tsutils. While tsutils offers utility functions, ts-morph provides a more comprehensive and user-friendly interface for manipulating TypeScript code.
babel-plugin-transform-typescript is a Babel plugin that allows Babel to transform TypeScript code. It is used for compiling TypeScript to JavaScript. Unlike tsutils, which provides utility functions for TypeScript AST and types, this plugin focuses on the transformation of TypeScript code during the build process.
This package consists of two major parts: utilities and typeguard functions. By importing the project you will get both of them.
import * as utils from "tsutils";
utils.isIdentifier(node); // typeguard
utils.getLineRanges(sourceFile); // utilities
If you don't need everything offered by this package, you can select what should be imported. The parts that are not imported are never read from disk and may save some startup time and reduce memory consumtion.
If you only need typeguards you can explicitly import them:
import { isIdentifier } from "tsutils/typeguard";
// You can even distiguish between typeguards for nodes and types
import { isUnionTypeNode } from "tsutils/typeguard/node";
import { isUnionType } from "tsutils/typeguard/type";
If you only need the utilities you can also explicitly import them:
import { forEachComment, forEachToken } from "tsutils/util";
This package is backwards compatible with typescript 2.8.0 at runtime although compiling might need a newer version of typescript installed.
Using typescript@next
might work, but it's not officially supported. If you encounter any bugs, please open an issue.
For compatibility with older versions of TypeScript typeguard functions are separated by TypeScript version. If you are stuck on typescript@2.8
, you should import directly from the submodule for that version:
// all typeguards compatible with typescript@2.8
import { isIdentifier } from "tsutils/typeguard/2.8";
// you can even use nested submodules
import { isIdentifier } from "tsutils/typeguard/2.8/node";
// all typeguards compatible with typescript@2.9 (includes those of 2.8)
import { isIdentifier } from "tsutils/typeguard/2.9";
// always points to the latest stable version (2.9 as of writing this)
import { isIdentifier } from "tsutils/typeguard";
import { isIdentifier } from "tsutils";
// always points to the typeguards for the next TypeScript version (3.0 as of writing this)
import { isIdentifier } from "tsutils/typeguard/next";
Note that if you are also using utility functions, you should prefer the relevant submodule:
// importing directly from 'tsutils' would pull in the latest typeguards
import { forEachToken } from 'tsutils/util';
import { isIdentifier } from 'tsutils/typeguard/2.8';
FAQs
utilities for working with typescript's AST
We found that tsutils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.